﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Qplus.protocol
{
    /* 定义抽象类，不能实例化*/
    public abstract class InPacket : TCPPackage
    {

        private int offset = 0;
        protected byte[] data;

        public InPacket()
        {
        }
        
	    public void handle(byte[] data, int offset, int count){
		    this.offset = offset;
		    parseHead(data, this.offset, count);
		    this.offset += 0;
		    parseBody(data, offset, count);
	    }


        /**s
	    * 解析头
	    * @param data
	    * @param offset
	    * @param count
	    */
        protected void parseHead(byte[] data, int offset, int count)
        {
            this.offset += 0;
        }

        /**
	    * 解析包体
	    * @param data
	    * @param offset
	    * @param count
	    */
        protected abstract void parseBody(byte[] data, int offset, int count);


        /**
	    * 获取一个字节
	    * @param data
	    * @return
	    */
        protected int get1(byte[] data)
        {
            return DataUtil.getInt(data[this.offset++]);
        }


        /**
	    * 获取二个字节
	    * @param data
	    * @return
	    */
        protected int get2(byte[] data)
        {
            return DataUtil.byteToShort(data[this.offset++], data[this.offset++]);
        }

        /**
         * 获取四个字节
         * @param data
         * @return
         */
        protected long get4(byte[] data)
        {
            return DataUtil.byteToInt(data[this.offset++], data[this.offset++], data[this.offset++], data[this.offset++]);
        }

        /**
         * 获取N个字节
         * @param data
         * @return
         */
        protected byte[] getN(byte[] data, int len)
        {
            byte[] n = new byte[len];

            Array.Copy(data, this.offset, n, 0, len); 
      
            //System.arraycopy(data, this.offset, n, 0, len);
            this.offset += len;
            return n;
        }

        /**
	    * 获得数据流
	    * @return
	    */
        public byte[] getData()
        {
            return data;
        }

        public void setData(byte[] data)
        {
            this.data = data;
        }

    }
}
